#include <iostream>
#include <vector>


using namespace std;

// 定义二叉树节点结构
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int findInorderIndex(const vector<int>& inorder, int start, int end, int value) {
    for (int i = start; i <= end; ++i) {
        if (inorder[i] == value) {
            return i;
        }
    }
    return -1;
}

TreeNode* buildTree(const vector<int>& preorder, const vector<int>& inorder, int preStart, int preEnd, int inStart, int inEnd) {
    if (preStart > preEnd || inStart > inEnd) {
        return nullptr;
    }

    TreeNode* root = new TreeNode(preorder[preStart]);
    int inIndex = findInorderIndex(inorder, inStart, inEnd, root->val);

    int leftTreeSize = inIndex - inStart;

    root->left = buildTree(preorder, inorder, preStart + 1, preStart + leftTreeSize, inStart, inIndex - 1);
    root->right = buildTree(preorder, inorder, preStart + leftTreeSize + 1, preEnd, inIndex + 1, inEnd);

    return root;
}


int maxDepth(TreeNode* root) {
    if (root == nullptr) {
        return 0;
    } else {
        int leftHeight = maxDepth(root->left);
        int rightHeight = maxDepth(root->right);
        return max(leftHeight, rightHeight) + 1;
    }
}

int main() {
    vector<char> preorder;
    vector<char> inorder;
    char c=getchar();
    while (c!='\n'){
        if (c != ' '){
        preorder.push_back(c);}
        c=getchar();
    }
    c=getchar();
    while (c!='\n'){
        if (c != ' '){
        inorder.push_back(c);}
        c=getchar();
    }


    TreeNode* root = buildTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
    int height = maxDepth(root);
    cout << height << endl;

    return 0;
}